<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>esd管理系统</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="/backend/plugins/element-ui/index.css">
    <link rel="stylesheet" href="/backend/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/backend/styles/list.css">

</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>生产线列表</h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>终端管理</el-breadcrumb-item>
            <el-breadcrumb-item>生产线列表</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <el-row>
                <el-col :span="24">
                    <div style="margin-left: 10px;">
                        <el-button type="primary" icon="el-icon-plus" plain class="butT" @click="handleCreate()">添加</el-button>
                    </div>
                </el-col>
            </el-row>
            <el-table
                    size="small"
                    current-row-key="lineId"
                    :data="dataList"
                    highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="lineName" label="生产线名称" align="center"></el-table-column>
                <el-table-column prop="lineDesc" label="生产线描述" align="center"></el-table-column>
                <!-- <el-table-column prop="viewOn" label="是否开启监控" align="center">
                    <template slot-scope="scope">
                        <el-switch
                                v-model="scope.row.viewOn"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                disabled
                        >
                        </el-switch>
                    </template>
                </el-table-column> -->
                <el-table-column label="操作" align="center" width="550">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <!-- <el-button v-if="scope.row.viewOn" type="warning" size="mini" @click="isViewOn(scope.row.lineId , '0')">关闭监控</el-button>
                        <el-button v-else type="primary" size="mini" @click="isViewOn(scope.row.lineId , '1')">开启监控</el-button> -->
                        <el-button type="success" size="mini" @click="showDashBoard(scope.row)">显示看板</el-button>
                        <el-button type="info" size="mini" @click="handleModule(scope.row)">设置模型</el-button>
                        <el-button type="warning" size="mini" @click="handlePositions(scope.row)">设置站点位置</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="120px">
                        <el-row>
                            <el-form-item label="生产线名称" prop="lineName">
                                <el-input v-model="formData.lineName"/>
                            </el-form-item>
                            <el-form-item label="生产线描述" prop="lineDesc">
                                <el-input
                                        type="textarea"
                                        :rows="3"
                                        placeholder="请输入内容"
                                        v-model="formData.lineDesc">
                                </el-input>
                            </el-form-item>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="cancelOpt()">取消</el-button>
                        <el-button type="primary" @click="saveOrUpdate()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="/backend/plugins/vue/vue.js"></script>
<script src="/backend/plugins/element-ui/index.js"></script>
<script type="text/javascript" src="/backend/plugins/jquery/jquery.min.js"></script>
<script src="/backend/plugins/axios/axios.min.js"></script>
<script src="/backend/js/request.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            dialogTitle: null ,
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                lineName: [{required: true, message: '生产线名称为必填项', trigger: 'blur'},
                    {pattern: '[^ \x22]+', message: '生产线名称不能为空'}],
                lineDesc: [{required: true, message: '生产线描述为必填项', trigger: 'blur'},
                    {pattern: '[^ \x22]+', message: '生产线描述不能为空'}]
            }
        },
        created() {
            this.findAll();
        },
        methods: {
            saveOrUpdate(){
                if(this.dialogTitle === '新增生产线信息'){
                    this.handleAdd();
                }else if (this.dialogTitle === '编辑生产线信息'){
                    this.handleEdit();
                }
            },
            //编辑
            handleEdit() {
                //进行表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        this.formData.lineName = this.formData.lineName.trim();
                        this.formData.lineDesc = this.formData.lineDesc.trim();
                        $axios({
                            url: '/line/updateLine',
                            method: 'put',
                            data: this.formData
                        }).then((res) => {
                            if (res.code === 0) {
                                this.$message({ type: 'success', message: res.msg });
                            } else {
                                this.$message.error(res.msg);
                            }
                        }).finally(() => {
                            this.findAll();
                            this.dialogFormVisible = false;
                            this.formData = {}
                        });
                    } else {
                        //表单校验不通过
                        this.$message.error("表单数据校验失败!");
                        return false;
                    }
                });
            },
            //添加
            handleAdd() {
                //进行表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        this.formData.lineName = this.formData.lineName.trim();
                        this.formData.lineDesc = this.formData.lineDesc.trim();
                        $axios({
                            url: '/line/addLine',
                            method: 'post',
                            data: this.formData
                        }).then((res) => {
                            if (res.code === 0) {
                                this.findAll();
                                this.$message({ type: 'success', message: res.msg });
                            } else {
                                this.$message.error(res.msg);
                            }
                            this.dialogFormVisible = false;
                        });
                    } else {
                        //校验不通过
                        this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                        return false;
                    }
                });
            },
            //分页查询
            findAll() {
                axios.get("/line/listAll").then((res) => {
                    this.dataList = res.data.data;
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};//重置数据
            },
            // 弹出添加窗口
            handleCreate() {
                this.dialogTitle = '新增生产线信息'
                //弹出新增窗口
                this.dialogFormVisible = true;
                this.resetForm();
            },
            cancelOpt() {
                //关闭新增窗口
                this.dialogFormVisible = false;
                this.resetForm();
                this.$message("已取消操作!!");
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.dialogTitle = '编辑生产线信息'
                //弹出编辑窗口
                this.dialogFormVisible = true;
                $axios({
                    url: '/line/getById/' + row.lineId,
                    method: 'get',
                    params: null
                }).then((res) => {
                    if (res.code === 0) {
                        this.formData = res.data;
                    } else {
                        this.$message.error(res.msg);
                    }
                });
            },
            // 删除
            handleDelete(row) {
                this.$confirm("你确定要删除当前数据吗？", "提示", {//确认框
                    type: 'warning'
                }).then(() => {
                    $axios({
                        url: "/line/deleteLine/" + row.lineId,
                        method: 'delete',
                        data: null
                    }).then((res) => {
                        if (res.code === 0) {
                            this.$message({ type: 'success',  message: res.msg });
                            this.findAll();
                        } else {
                            this.$message.error(res.msg);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '操作已取消'
                    });
                });
            },
            handleModule(row){
                window.open("/backend/page/line/setModule.html?line=" + row.lineName);
            },
            handlePositions(row){
                window.open("/backend/page/line/setPositions.html?line=" + row.lineName);
            },
            showDashBoard(row){
                window.open("/backend/page/line/dashBoard.html?line=" + row.lineName);
            },
            // isViewOn(lineId , value){
            //     let str = (value===0)?"关闭":"开启";
            //     $axios({
            //         url: '/line/setViewOn/' + lineId + "/" + value,
            //         method: 'get',
            //         params: null
            //     }).then((res) => {
            //         if (res.code === 0) {
            //             this.$message.success(str + "监控成功！！");
            //             this.findAll();
            //         } else {
            //             this.$message.error(str + "监控失败！！");
            //         }
            //     });
            // }
        },
    })
</script>
</html>